package com.iflysse.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iflysse.config.ConfigParam;
import com.iflysse.dao.StudentTaskDao;
import com.iflysse.dto.StudentCompleteTaskAllDto;
import com.iflysse.dto.StudentCompleteTaskCorrectDto;
import com.iflysse.dto.StudentCompleteTaskDto;
import com.iflysse.dto.StudentTaskDto;
import com.iflysse.service.StudentTaskService;
import com.iflysse.utils.CommonUtil;
import com.iflysse.vo.TaskLearnRecordVo;

@Service
public class StudentTaskServiceImpl implements StudentTaskService {

	@Autowired
	private StudentTaskDao studentTaskDao;
	@Autowired
	private ConfigParam configParam;

	@Override
	public Object getWaitingTask(Integer pageNum, Integer pageSize, String studentId) {
		Page<StudentTaskDto> pa = PageHelper.startPage(pageNum, pageSize);
		studentTaskDao.getWaitingTask(studentId);
		TaskLearnRecordVo studentTaskVo = CommonUtil.objToObj(pa.toPageInfo(), TaskLearnRecordVo.class);
		// 设置图片路劲
		for (int i = 0; i < pa.toPageInfo().getList().size(); i++) {
			pa.toPageInfo().getList().get(i).setCourseCover(
					configParam.getStaticResourcePrefix() + pa.toPageInfo().getList().get(i).getCourseCover());
		}
		// pa.toPageInfo() 没有的字段自己添加
		studentTaskVo.setList(pa.toPageInfo().getList());
		studentTaskVo.setTotal(pa.toPageInfo().getTotal());

		return studentTaskVo;
	}

	@Override
	public Object getQueryCompleteTask(Integer pageNum, Integer pageSize, String studentId) {
		Page<StudentCompleteTaskAllDto> pa = PageHelper.startPage(pageNum, pageSize);
		List<StudentCompleteTaskAllDto> allDtos = studentTaskDao.getQueryCompleteTask(studentId);
		// 反射整理数据
		List<StudentCompleteTaskDto> taskDtos = new ArrayList<>();
		StudentCompleteTaskCorrectDto taskCorrectDto = new StudentCompleteTaskCorrectDto();
		for (StudentCompleteTaskAllDto allDto : allDtos) {
			StudentCompleteTaskDto completeTaskDto = CommonUtil.objToObj(allDto, StudentCompleteTaskDto.class);
			// completeTaskDto.setCorrectRateOfnowledgeList(CommonUtil.objToObj(allDto,
			// StudentCompleteTaskCorrectDto.class));
			// StudentCompleteTaskAllDto 中的字段不是一般get set 手动设置
			taskCorrectDto.setAvgCorrectRate(allDto.getAvgCorrectRate());
			taskCorrectDto.setCorrectRate(allDto.getAvgCorrectRate());
			taskCorrectDto.setKnowledgeName("编程题");
			completeTaskDto.setCorrectRateOfKnowledgeList(taskCorrectDto);
			taskDtos.add(completeTaskDto);
		}
		TaskLearnRecordVo studentTaskVo = CommonUtil.objToObj(pa.toPageInfo(), TaskLearnRecordVo.class);

		// 设置图片路径
		for (int i = 0; i < taskDtos.size(); i++) {
			taskDtos.get(i).setCourseCover(configParam.getStaticResourcePrefix() + taskDtos.get(i).getCourseCover());
		}
		// pa.toPageInfo() 没有的字段自己添加
		studentTaskVo.setList(taskDtos);
		studentTaskVo.setTotal(pa.toPageInfo().getTotal());
		studentTaskVo.setStart(1);
		return studentTaskVo;
	}

	@Override
	public Object getTopFourWaitingTask(Integer pageNum, Integer pageSize, String studentId) {
		PageHelper.startPage(pageNum, pageSize);
		List<StudentTaskDto> list = studentTaskDao.getTopFourWaitingTask(studentId);
		// 设置图片路径
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setCourseCover(configParam.getStaticResourcePrefix() + list.get(i).getCourseCover());
		}

		return list;
	}

	@Override
	public Object getQueryTopFourCompleteTask(Integer pageNum, Integer pageSize, String studentId) {
		Page<StudentCompleteTaskAllDto> pa = PageHelper.startPage(pageNum, pageSize);
		List<StudentCompleteTaskAllDto> allDtos = studentTaskDao.getQueryTopFourCompleteTask(studentId);
		// 反射整理数据
		List<StudentCompleteTaskDto> taskDtos = new ArrayList<>();
		StudentCompleteTaskCorrectDto taskCorrectDto = new StudentCompleteTaskCorrectDto();
		// 设置图片路径
		for (int i = 0; i < allDtos.size(); i++) {
			allDtos.get(i).setCourseCover(configParam.getStaticResourcePrefix() + allDtos.get(i).getCourseCover());
		}
		for (StudentCompleteTaskAllDto allDto : allDtos) {
			StudentCompleteTaskDto completeTaskDto = CommonUtil.objToObj(allDto, StudentCompleteTaskDto.class);
			// completeTaskDto.setCorrectRateOfnowledgeList(CommonUtil.objToObj(allDto,
			// StudentCompleteTaskCorrectDto.class));
			// StudentCompleteTaskAllDto 中的字段不是一般get set 手动设置
			taskCorrectDto.setAvgCorrectRate(allDto.getAvgCorrectRate());
			taskCorrectDto.setCorrectRate(allDto.getAvgCorrectRate());
			taskCorrectDto.setKnowledgeName("编程题");
			completeTaskDto.setCorrectRateOfKnowledgeList(taskCorrectDto);
			taskDtos.add(completeTaskDto);
		}

		return taskDtos;
	}

}
